﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using BemEstarEmprego.BLL;
using BemEstarEmprego.BLL.Models;
using BemEstarEmprego.Entities;
//using BemEstarEmprego.Web.Models;
using BemEstarEmprego.Web.Helpers;

namespace BemEstarEmprego.Web.Controllers
{
	[SecurityFilter]
	public class EmpresaController : BaseController
	{

		public ActionResult Index()
		{
			return View();
		}

		public ActionResult Editar(String id)
		{
			ViewBag.Action = "Alterar";
			using (bemestar_empregoEntities context = new bemestar_empregoEntities())
			{
				Int32 _id = Convert.ToInt32(id);

				EmpresaModel model = new EmpresaModel();

				model._empresa = (from _empresa in context.empresa
								  where _empresa.ID == _id
								  select _empresa).FirstOrDefault<empresa>();

				model._empresa.enderecoReference.Load();

				model._endereco = (from _endereco in context.endereco
								   where _endereco.ID == model._empresa.endereco.ID
								   select _endereco).FirstOrDefault<endereco>();

				return View(model);
			}
		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Salvar")]
		public ActionResult Salvar(EmpresaModel model, EnderecoModel modelEndereco)
		{
			EmpresaBLL empresaBLL = new EmpresaBLL();
			EnderecoBLL enderecoBLL = new EnderecoBLL();

			model._empresa.endereco = modelEndereco._endereco;
			var retornoSave = empresaBLL.Save(model._empresa);

			return RedirectToAction("list");
		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Alterar")]
		public ActionResult Alterar(EmpresaModel model)
		{
			EmpresaBLL empresa = new EmpresaBLL();
			empresa.UpdateEmpresa(model._empresa);

			EnderecoBLL endereco = new EnderecoBLL();
			endereco.UpdateEndereco(model._endereco);

			return RedirectToAction("list");
		}


		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Desativar")]
		public ActionResult Desativar(EmpresaModel _model)
		{
			EmpresaBLL empresa = new EmpresaBLL();

			if (!Convert.ToBoolean(_model._empresa.ATIVO))
				_model._empresa.ATIVO = true;
			else if (Convert.ToBoolean(_model._empresa.ATIVO))
			{
				_model._empresa.ATIVO = false;
			}

			empresa.DesativarAtivar(_model._empresa);
			return RedirectToAction("list");
		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Voltar")]
		public ActionResult Voltar()
		{
			return RedirectToAction("list");
		}

		public ActionResult Cadastro()
		{
			ViewBag.Action = "Salvar";
			return View();
		}

		[HttpGet]
		public ActionResult List()
		{
			string pageString = "";

			try
			{
				pageString = Request.Url.Segments[3];
			}
			catch (Exception)
			{
				pageString = null;
			}

			int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);

			int totalEmpresa = new EmpresaModel().GetTotalEmpresas();

			Extension.Pagination pagination = new Extension.Pagination(true);


			pagination.BaseUrl = "/empresa/List/";
			pagination.TotalRows = totalEmpresa;
			pagination.CurPage = page;
			pagination.PerPage = 10;

			pagination.PrevLink = "Prev";
			pagination.NextLink = "Next";

			string pageLinks = pagination.GetPageLinks();

			int start = (page - 1) * pagination.PerPage;
			int offset = pagination.PerPage;

			List<empresa> empresas = new EmpresaModel().GetEmpresas(start, offset);

			ViewData["title"] = "Pagination in Asp.Net Mvc";
			ViewData["totalEmpresa"] = totalEmpresa;
			ViewData["empresas"] = empresas;
			ViewData["pageLinks"] = pageLinks;
			ViewBag.Links = pageLinks;

			return View("Consulta");
		}

	}
}
